package com.atguigu.gulimall.cart.controller;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.cart.entry.vo.CartItemVo;
import com.atguigu.gulimall.cart.entry.vo.CartVo;
import com.atguigu.gulimall.cart.entry.vo.UserInfoTo;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@Controller
public class CartController {

    @Autowired
    CartService cartService;
    /**
     * 浏览器有一个cokie：user-key；标示用户身份，一个月后过期；
     * 如果第一次使用购物车功能，都会给临时用户分配一个临时身份
     * 浏览器以后保存，每次访问都会带上这个cookie
     *
     * 登录：session有
     * 没登录：按照cokie里带来的user-key
     * 第一次如果没有临时用户，需要创建一个临时用户；
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {

        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);

        return "cartList";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check){

        cartService.checkItem(skuId,check);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){

        cartService.countItem(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deleteItem(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 获取当前用户所有选中的购物项目
     * @return
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> getRurrentUserCartItems(){

        List<CartItemVo> itemVoList = cartService.getUserCartItems();

        return itemVoList;
    }


    /**
     * 添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes attributes) throws ExecutionException, InterruptedException {

//        CartItemVo cartItemVo = cartService.addToCart(skuId,num);
//        model.addAttribute("item",cartItemVo);

        cartService.addToCart(skuId,num);
        //重定向携带数据用RedirectAttributes addFlashAttribute添加的数据只能取一次
        attributes.addAttribute("skuId",skuId);
        //需要重定向到成功页面，防止刷新重复提交
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页，多次刷新，只做查询操作
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,
                                       Model model){
        //重定向到成功页面，再次查询购物车数据即可
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItemVo);

        return "success";
    }
}
